<!doctype html>

<html>

<head>

	<meta charset="utf-8">

	<meta name="apple-itunes-app" content="app-id=468944728">

	<meta id="viewport" name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,initial-scale=1.0,user-scalable=no">

	<title>授权页</title>

	<head>

		<script type="text/javascript" src="../plug/jquery-easyui/jquery-1.8.0.min.js"></script>

		<script>



			var appid = '';

			var secret = '';

			var weixinId = getUrlParam("weixinId");

			var productId = getUrlParam("productId");

			var url_ = 'http://mwx.chinatoplady.cn/pages/authProduct.html?weixinId='+weixinId+'&productId='+productId;

			var redirect_uri = encodeURI(url_);

			var code = '';



			//根据微信ID，获取微信公众账号信息

			$.post('http://mwx.chinatoplady.cn/weixin/getWeixinConfigById?weixinId='+weixinId,function(data){



				appid = jQuery.trim(data.appId);

				secret = jQuery.trim(data.appSecret);



				code = getUrlParam("code");

				if(code==null){

					window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+appid+"&redirect_uri="+redirect_uri+"&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";

				}else{

					//alert('appid:'+appid+',secret:'+secret+',code:'+code+',grant_type:authorization_code');

					//获取当前微信用户信息

					$.ajax({

						type:'post',

						dataType:'json',

						url:'http://mwx.chinatoplady.cn/weixin/getWxUserInfo',

						data:{'appid':appid,'secret':secret,'code':code,'grant_type':'authorization_code'},

						async:false,

						success:function(data){

							var result = JSON.parse(data.result);

							if(result.errcode == 48001){

								window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+appid+"&redirect_uri="+redirect_uri+"&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";

							}else{

								if(result.openid == undefined || result.openid == 'undefined'){

									window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+appid+"&redirect_uri="+redirect_uri+"&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";

								}else{

									//alert("shopId="+shopId+",externalNo="+result.openid+",externalName="+result.nickname+",logo="+result.headimgurl+',shopId='+shopId);

									$.ajax({

										type:'post',

										dataType:'json',

										url:"http://mwx.chinatoplady.cn/weixin/getMemberByOpenId",

										data:{'openId':result.openid},

										async:false,

										success:function(data){

											if(data!=null){

												$.ajax({

													type:'post',

													dataType:'json',

													url:"http://mwx.chinatoplady.cn/weixin/addMember",

													data:{"nickName":result.nickname,"openId":result.openid,"logo":result.headimgurl,"sex":result.sex,"productId":productId},

													async:false,

													success:function(data){

														if(data=="success"){

															//这里直接跳到微商城   也就是商品详情
															window.location.href = 'http://mwx.chinatoplady.cn/wx/index/info';

														}else{

															alert('系统异常3');

														}



													},

													error:function(){

														alert('系统异常4');

													}

												});

											}else{

												alert('系统异常1');

											}



										},

										error:function(){

											alert('系统异常2');

										}

									});

								}



							}



						},

						error:function(){

							alert('系统异常3');

						}



					})

				}



			})





			//地址栏获取参数

			function getUrlParam(name){

				var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");

				var r = window.location.search.substr(1).match(reg);

				if (r!=null)

					return unescape(r[2]);

				return null;

			}



		</script>

	</head>

<body></body>

</html>